﻿@page "/keywords/{id}"
@page "/keywords/{id}/pages/{pageNumber:int}"
@using AcBlog.Data.Models.Builders
@inherits BaseKeywordPage
@inject AcBlog.Services.Filters.PostKeywordFilter Keywords

<PageContainer Title="@(Keyword.OneNameOrDefault())" PageTitle="@Title">
    <Breadcrumb>
        <Breadcrumb>
            <BreadcrumbItem>
                <a href="">
                    <MdiIcon Type="@IconNames.Home" />
                    <span>Home</span>
                </a>
            </BreadcrumbItem>
            <BreadcrumbItem>
                <a href="@UrlGenerator.Keywords()">
                    <MdiIcon Type="@IconNames.Keywords" />
                    <span>Keywords</span>
                </a>
            </BreadcrumbItem>
            <BreadcrumbItem>
                <span>@(Keyword.OneNameOrDefault())</span>
            </BreadcrumbItem>
        </Breadcrumb>
    </Breadcrumb>
    <ChildContent>
        <Loader OnLoad="@OnLoad" LoadKey="@($"{Id}-{PageNumber}")" @ref="loader">
            <PagingList Data="@Data" Column="1" PageNavigationUrl="@PageNavigationUrl">
                <ItemTemplate>
                    <PostListItem Id="@context" ShowType="true"></PostListItem>
                </ItemTemplate>
            </PagingList>
        </Loader>
    </ChildContent>
</PageContainer>

@code {
    const int PageSize = 10;

    PagingData<string> Data { get; set; }

    [Parameter]
    public string Id { get; set; }

    [Parameter]
    public int? PageNumber { get; set; } = null;

    private Keyword Keyword = Keyword.Empty;

    private Loader loader;

    private string PageNavigationUrl;

    private async Task OnLoad()
    {
        Title = "Loading...";
        Keyword = AcBlog.Data.Models.Keyword.Parse(Id);
        PageNavigationUrl = UrlGenerator.Keyword(Keyword);
        Data = await Keywords.Filter(Keyword, new Data.Models.Actions.Pagination
        {
            CurrentPage = PageNumber ?? 0,
            PageSize = PageSize
        }, QueryTimeOrder.CreationTimeDescending);
        Title = $"Page {Data.CurrentPage.CurrentPage + 1} - {Keyword.ToString()}";
    }
}
